package com.geek.javase.ch14.work.threadstate;

/*
 *@author wangpeng
 *2025/10/30 22:31
 *@description TODO
 */
public class ThreadStateDemo {
    public static void main(String[] args) throws InterruptedException {
        // 验证所有线程状态
        demonstrateNewState();
        demonstrateRunnableState();
        demonstrateBlockedState();
        demonstrateWaitingState();
        demonstrateTimedWaitingState();
        demonstrateTerminatedState();
    }

    // 1. NEW状态 - 线程创建但未启动
    static void demonstrateNewState() {
        Thread thread = new Thread(() -> {
            System.out.println("线程执行中...");
        });
        System.out.println("1. NEW状态: " + thread.getState());
    }

    // 2. RUNNABLE状态 - 线程可运行或正在运行
    static void demonstrateRunnableState() throws InterruptedException {
        Thread thread = new Thread(() -> {
            // 模拟一些工作
            for (int i = 0; i < 1000000; i++) {
                Math.sqrt(i);
            }
        });

        thread.start();
        Thread.sleep(10); // 给线程一点时间启动
        System.out.println("2. RUNNABLE状态: " + thread.getState());

        thread.join(); // 等待线程结束
    }

    // 3. BLOCKED状态 - 线程等待监视器锁
    static void demonstrateBlockedState() throws InterruptedException {
        Object lock = new Object();

        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    Thread.sleep(2000); // 持有锁2秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("线程2获取到锁");
            }
        });

        thread1.start();
        Thread.sleep(100); // 确保thread1先启动

        thread2.start();
        Thread.sleep(100); // 确保thread2启动并尝试获取锁

        System.out.println("3. BLOCKED状态: " + thread2.getState());

        thread1.join();
        thread2.join();
    }

    // 4. WAITING状态 - 无限期等待
    static void demonstrateWaitingState() throws InterruptedException {
        Object lock = new Object();

        Thread thread = new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait(); // 进入WAITING状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
        Thread.sleep(100); // 确保线程启动并进入wait()

        System.out.println("4. WAITING状态: " + thread.getState());

        // 唤醒线程
        synchronized (lock) {
            lock.notify();
        }

        thread.join();
    }

    // 5. TIMED_WAITING状态 - 有限时间等待
    static void demonstrateTimedWaitingState() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(5000); // 睡眠5秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread.start();
        Thread.sleep(100); // 确保线程启动并进入sleep()

        System.out.println("5. TIMED_WAITING状态: " + thread.getState());

        thread.join();
    }

    // 6. TERMINATED状态 - 线程执行完成
    static void demonstrateTerminatedState() throws InterruptedException {
        Thread thread = new Thread(() -> {
            // 简单任务
            System.out.println("线程执行任务");
        });

        thread.start();
        thread.join(); // 等待线程结束

        System.out.println("6. TERMINATED状态: " + thread.getState());
    }
}